<template>
    <div class="fillContainer">
        <div>
            <el-form :inline="true" ref="add_data" :model="search_data">
                <!-- 筛选 -->
                <el-form-item label="按照时间筛选：">
                    <el-date-picker v-model="search_data.startTime" type="datetime" placeholder="选择开始时间">
                    </el-date-picker>
                    --
                    <el-date-picker v-model="search_data.endTime" type="datetime" placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" icon="search" @click="handleSearch()">筛选</el-button>
                </el-form-item>
                <el-form-item class="btnRight">
                    <el-button type="primary" v-if="user.identity == 'manager'" size="small" icon="view"
                        @click="handleAdd()">添加</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 表格 -->
        <div class="table_container">
            <el-table v-if="tableData && tableData.length > 0" :data="tableData" max-height="450" border>
                <el-table-column prop="id" label="序号" width="40" align="center"></el-table-column>
                <el-table-column prop="date" label="创建时间" width="226" align="center">
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span style="margin-left:10px">{{ tempToData(scope.row.date) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="收支类型" width="140" align="center"></el-table-column>
                <el-table-column prop="describe" label="收支描述" width="160" align="center"></el-table-column>
                <el-table-column prop="income" label="收入" width="140" align="center">
                    <template slot-scope="scope">
                        <span style="color:#00d053">{{ '+' + scope.row.income }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="expend" label="支出" width="140" align="center">
                    <template slot-scope="scope">
                        <span style="color:#f56767">{{ '-' + scope.row.expend }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="cash" label="账户现金" width="140" align="center">
                    <template slot-scope="scope">
                        <span style="color:#4db3ff">{{ scope.row.cash }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" width="190" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="150" prop="operation">
                    <template slot-scope="scope">
                        <el-button type="warning" v-if="user.identity == 'manager'" icon="edit" size="small"
                            @click="handleEdit(scope.$index, scope.row)">
                            编辑
                        </el-button>
                        <el-button size="small" v-if="user.identity == 'manager'" icon="delete" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-row>
                <el-col :span="24">
                    <div class="pagination">
                        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                            :current-page.sync="paginations.page_index" :page-sizes="paginations.page_sizes"
                            :page-size="paginations.page_size" :layout="paginations.layout" :total="paginations.total">
                        </el-pagination>
                    </div>
                </el-col>
            </el-row>
        </div>

        <Dialog :dialog="dialog" :formData="formData" @update="getProfile"></Dialog>
    </div>
</template>

<script>
import Dialog from '@/components/Dialog.vue';
export default {
    name: 'fundList',
    components: {
        Dialog
    },
    data() {
        return {
            search_data: {
                startTime: '',
                endTime: ''
            },
            filterTableData: [],
            //分页
            paginations: {
                page_index: 1,//当前位于哪页
                total: 0,//总数
                page_size: 5,//一页显示多少条
                page_sizes: [5, 10, 15, 20],//每页显示多少条
                layout: 'total,sizes,prev,pager,next,jumper'//翻页属性
            },
            //表格数据
            allTableData: {},
            tableData: {},
            //添加、编辑表单
            formData: {
                type: '',
                describe: '',
                income: '',
                expend: '',
                cash: '',
                remark: '',
            },
            //表单状态
            dialog: {
                show: false,
                title: '',
                option: 'edit'
            }
        }
    },
    computed: {
        user() {
            return this.$store.getters.user
        },
    },
    created() {
        this.getProfile();
    },
    methods: {
        getProfile() {
            //获取表格数据
            this.$axios.get('http://127.0.0.1:8080/profile').then(res => {
                this.allTableData = res.data.data
                this.filterTableData = res.data.data
                //设置分页数据
                this.setPaginations()
            }).catch(err => {
                console.log(err);
            })
        },
        setPaginations() {
            //分页属性设置
            this.paginations.total = this.allTableData.length
            this.paginations.page_index = 1
            this.paginations.page_size = 5
            //设置默认分页数据
            this.tableData = this.allTableData.filter((item, index) => {
                return index < this.paginations.page_size
            })
        },
        //切换页数范围
        handleSizeChange(page_size) {
            //切换size
            this.paginations.page_index = 1
            this.paginations.page_size = page_size
            this.tableData = this.allTableData.filter((item, index) => {
                return index < page_size
            })
        },
        //选择页数
        handleCurrentChange(page) {
            //获取当前页
            let index = this.paginations.page_size * (page - 1)
            //数据总数
            let nums = this.paginations.page_size * page
            //容器
            let tables = []
            for (let i = index; i < nums; i++) {
                if (this.allTableData[i]) {
                    tables.push(this.allTableData[i])
                }
                this.tableData = tables
            }
        },
        // 筛选
        handleSearch() {
            if (!this.search_data.startTime || !this.search_data.endTime) {
                this.$message({
                    type: 'warning',
                    message: '请选择时间区间'
                })
                this.getProfile()
                return;
            }
            const sTime = this.search_data.startTime.getTime()
            const eTime = this.search_data.endTime.getTime()

            this.allTableData = this.filterTableData.filter(item => {
                let date = new Date(item.date)
                let time = date.getTime()
                return time >= sTime && time <= eTime
            })
            //分页数据
            this.setPaginations()
        },
        //编辑
        handleEdit(index, row) {
            //编辑
            this.dialog = {
                show: true,
                title: '修改资金信息',
                option: 'edit'
            },
                // console.log(index,row.id);
                this.formData = {
                    type: row.type,
                    describe: row.describe,
                    income: row.income,
                    expend: row.expend,
                    cash: row.cash,
                    remark: row.remark,
                    id: row.id
                }
        },
        //删除
        handleDelete(index, row) {
            //删除
            this.$confirm('确认删除？', '系统提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                cancelButtonClass: 'btn-custom-cancel',
                type: 'warning'
            }).then(() => {
                this.$axios.post(`http://127.0.0.1:8080/profile/delete/${row.id}`).then(res => {
                    this.$message('删除成功')
                    this.getProfile()
                })
            }).catch((err) => {
                console.log(err);
            })

        },
        //添加
        handleAdd() {
            //添加
            this.dialog = {
                show: true,
                title: '添加资金信息',
                option: 'add'
            },
                this.formData = {
                    type: '',
                    describe: '',
                    income: '',
                    expend: '',
                    cash: '',
                    remark: '',
                }
        },

        // 时间格式转换
        tempToData(unixtimestamp2) {
            var unixtimestamp = new Date(unixtimestamp2)
            var year = 1900 + unixtimestamp.getYear()
            var month = '0' + (unixtimestamp.getMonth() + 1)
            var date = '0' + unixtimestamp.getDate()
            var hour = '0' + unixtimestamp.getHours()
            var minute = '0' + unixtimestamp.getMinutes()
            var second = '0' + unixtimestamp.getSeconds()
            return year + '-' + month.substring(month.length - 2, month.length) + '-' + date.substring(date.length - 2, date.length) +
                ' ' + hour.substring(hour.length - 2, hour.length) + ':' +
                minute.substring(minute.length - 2, minute.length) + ':' +
                second.substring(second.length - 2, second.length)
        },

        //添加测试
        // handleAdd2(index, row) {
        //     this.formData = {
        //         date: this.tempToData(row.date),
        //         type: row.type,
        //         describe: row.describe,
        //         income: row.income,
        //         expend: row.expend,
        //         cash: row.cash,
        //         remark: row.remark,
        //         profile_id: row.id
        //     }
        //     this.$axios.post(`http://127.0.0.1:8080/profile/addtest`, this.formData).then(res => {
        //         this.$message('添加成功')

        //     })
        // }
    },
}
</script>

<style scoped>
.fillContainer {
    /* width: 100%; */
    padding: 16px;
    height: 100%;
}

.btnRight {
    float: right;
}

.pagination {
    text-align: right;
    margin-top: 10px;
}
</style>